package com.bhs.sansong.ui.discountCoupon;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.LogisticsDiscountCouponAdapter;
import com.bhs.sansong.base.BaseFragment;
import com.bhs.sansong.bean.CompanyInformation;
import com.bhs.sansong.bean.CouponSearchBean;
import com.bhs.sansong.bean.DedicatedLineBean;
import com.bhs.sansong.bean.DedicatedLineDataBean;
import com.bhs.sansong.bean.DiscountCouponBean;
import com.bhs.sansong.bean.MenuBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.ui.dialog.BottomSelectListDialog;
import com.bhs.sansong.ui.dialog.MyClickListener2;
import com.bhs.sansong.ui.shipments.IssueNewOrderActivity;
import com.bhs.sansong.utils.MyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

public class CouponReceivedFragment extends BaseFragment implements NetCallBack {

    private SwipeRefreshLayout mRefreshLayout;

    private LogisticsDiscountCouponAdapter mAdapter;
    private RecyclerView mRvList;
    private int page = 1;
    private String type;

    public static CouponReceivedFragment newInstance(String type) {
        CouponReceivedFragment upstreamShipmentListFragment = new CouponReceivedFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        upstreamShipmentListFragment.setArguments(bundle);
        return upstreamShipmentListFragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_coupon_received;
    }

    @Override
    public void initView(View root) {
        type = getArguments().getString("type");
        mRefreshLayout = root.findViewById(R.id.refreshLayout);
        mRvList = root.findViewById(R.id.rv_list);
    }

    @Override
    public void initData() {
        if (type.equals("0")) {
            mAdapter = new LogisticsDiscountCouponAdapter();
        } else {
            mAdapter = new LogisticsDiscountCouponAdapter(R.layout.item_have_expired, Integer.parseInt(type));
        }
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty, null));
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                DiscountCouponBean data = mAdapter.getData().get(position);
                if (TextUtils.isEmpty(data.getCompanyName())) {
                    String order_type = null;
                    if (data.getGetCouponInfo().getUse_type() > 0) {
                        order_type = String.valueOf(data.getGetCouponInfo().getUse_type());
                    } else {
                        //通用券默认显示极速
                        order_type = "2";
                    }
                    startActivity(new Intent(getContext(), IssueNewOrderActivity.class)
                            .putExtra("order_type", order_type));
                } else {
                    networkRequest(netApi.getSpecialList(page, 20, data.getLogistics_id()), CouponReceivedFragment.this, 88);
                }
            }
        });
        //监听 刷新是回调
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        loadData();
    }


    private void selectLine(List<DedicatedLineBean> list) {
        List<MenuBean> menuBeans = new ArrayList<>();
        for (DedicatedLineBean dedicatedLineBean : list) {
            String line = dedicatedLineBean.getStart_city() + " - " + dedicatedLineBean.getEnd_city();
            line = line.replace("市", "");
            menuBeans.add(new MenuBean(line));
        }

        BottomSelectListDialog listDialog = new BottomSelectListDialog(getContext(), menuBeans);
        listDialog.show();
        listDialog.setMyClickListener(new MyClickListener2() {
            @Override
            public void onClick(String id, String info) {
                DedicatedLineBean data = list.get(Integer.parseInt(info));
                CompanyInformation companyInfo = new CompanyInformation(data.getCompany_pic(),
                        data.getAddress(), String.valueOf(data.getLogistics_id()), data.getLine_id(), data.getCompany_name());
                companyInfo.setStartCity(data.getStart_city());
                companyInfo.setEndCity(data.getEnd_city());
                startActivity(new Intent(getContext(), IssueNewOrderActivity.class)
                        .putExtra("order_type", "1")
                        .putExtra("place_order_type", "1")
                        .putExtra("networkCity", data.getEnd_city())
                        .putExtra("companyInfo", companyInfo));
            }
        });
    }

    Map<String,Object> map=new ArrayMap<>();
    private void loadData() {
        map.put("status",type);
        map.put("page",page);
        networkRequest(netApi.couponSelfList(map), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 0) {
            CouponSearchBean bean = new Gson().fromJson(json, CouponSearchBean.class);
            mRefreshLayout.setRefreshing(false);
            if (bean.getCode().equals("1")) {
                if (page == 1) {
                    mAdapter.setNewData(bean.getData().getList());
                    EventBus.getDefault().post(bean.getData().getNumArr());
                } else {
                    mAdapter.loadMoreComplete();
                    mAdapter.addData(bean.getData().getList());
                }
                if (MyUtils.isEmpty(bean.getData().getList())) {
                    mAdapter.loadMoreEnd();
                }
            }
        } else {
            DedicatedLineDataBean bean = new Gson().fromJson(json, DedicatedLineDataBean.class);
            if (bean.isStatus()) {
                selectLine(bean.getData().getList());
            }
        }
    }


    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }
}
